<?php
namespace app\api\model;
use app\api\model\Common;
class Order extends Common
{
    /**
     * 订单状态切换 order_state
     * 订单状态，0：待审批，1：已批准，2：已拒绝，3：待用车，4：已派车，5：接驾中，
     * 6：已到达出发地，7：已到达目的地，8：待支付，9：待评价，10：已完成，11：已取消，12：已终止，13：无效
     * TODO 状态切换判断
    */
    public function changeStatus($oid,$status,$driver_id=0){
        if($driver_id>0){
            return $this->save(['order_state' => $status],['order_id' => $oid,'driver_id'=>$driver_id]);
        }else{
            return $this->save(['order_state' => $status],['order_id' => $oid]);
        }
    }

    public function receiveOrder($oid,$driver_id){
        return $this->save(['order_state' => 4,'driver_id'=>$driver_id],['order_id' => $oid,'driver_id'=>null]);
    }


	 /**
     * 获取订单列表
	 * @param int $driver_id 司机id
	 * @param int $car_type_id 车型id
	 * @param datetime $start_time 开始时间
	 * @param datetime $end_time 结束时间
	 * @param int $order_state 0:历史订单，1:当前订单
    */
    public function getOrderList($driver_id,$car_type_id,$start_time,$end_time,$order_state){
		$where['driver_id']=$driver_id;
		//订单状态
		if ($order_state)
			$where['order_state']=array('between','0,7');
		else
			$where['order_state']=array('between','8,13');
		//车型id
		if ($car_type_id)
			$where['car_type_id']=$car_type_id;
		//时间筛选
		if($start_time&&$end_time){
			$where['order_time']=array('between',$start_time.','.$end_time);
		}
		else{
			if($start_time)
			{
				$where['order_time']=array('egt',$start_time);
			}
			else if($end_time){
				$where['order_time']=array('elt',$end_time);
			}
		}
        return $this->where($where)->field('order_id,order_state,order_type,use_type,booking_type,order_time,car_type_id,departure,destination')->select();
    }

	/**
     * 获取订单列表
	 * @param int $order_id 订单id
    */
	public function getOrderDetail($order_id){
		return $this->where('order_id',$order_id)->select();
	}
}
